package thinkingJava.chapter18;

import static util.Print.*;

import java.io.FileOutputStream;
import java.nio.channels.FileLock;
import java.util.concurrent.TimeUnit;

public class FileLocking {
	public static void main(String[] args) throws Exception {
		FileOutputStream fos = new FileOutputStream("file.txt");
		FileLock fl = fos.getChannel().tryLock();
		print("FileLock.isShared() ? " + fl.isShared());
		if (fl != null) {
			print("Locked File");
			TimeUnit.MILLISECONDS.sleep(3000);
			fl.release();
			print("Release Lock");
		}
		fos.close();
	}
}
